package edu.beyond.employment.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import edu.beyond.employment.commom.Constant;
import edu.beyond.employment.commom.PageModel;
import edu.beyond.employment.commom.util.AppUtil;
import edu.beyond.employment.company.dao.ApplicationDao;
import edu.beyond.employment.company.dao.PositionDao;
import edu.beyond.employment.company.entity.ApplicationEntity;
import edu.beyond.employment.company.entity.PositionEntity;
import edu.beyond.employment.talent.dao.ResumeDao;
import edu.beyond.employment.talent.entity.ResumeEntity;

/**
 * ApplicationServlet控制层
 */
public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ApplicationDao applicationDao = new ApplicationDao();
	private ResumeDao resumeDao = new ResumeDao();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取页面传来的参数
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("delete".equals(command)) {
			delete(request, response);
		} else if ("get".equals(command)) {
			get(request, response);
		} else if ("apply".equals(command)) {
			apply(request, response);
		} else if ("listTalentApplyByPage".equals(command)) {
			listTalentApplyByPage(request, response);
		} else if ("listCompanyVerifyByPage".equals(command)) {
			listCompanyVerifyByPage(request, response);
		} else if ("verify".equals(command)) {
			verify(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 删除求职信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取页面传来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 调用业务逻辑
		int row = applicationDao.delete(id);
		// 转向页面
		if (row == 1) {
			request.setAttribute("success", "删除求职信息成功！");

		} else {
			request.setAttribute("fail", "删除求职信息失败！");
		}
		// 回到首页是显示简历信息
		int talentId = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		// List<ApplicationEntity> applicationList =
		// applicationDao.list(talentId);
		// request.setAttribute("applicationList", applicationList);
		int pageNo = Constant.DEFAULT_PAGE_NO;
		// 非登录操作
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		PageModel<ApplicationEntity> pageModel = applicationDao.listTalentApplyByPage(talentId, pageNo,
				Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("view/talent/talent-home.jsp").forward(request, response);

	}

	/**
	 * 投递简历
	 */
	private void apply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传来的参数
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		// 调用业务逻辑
		int talentId = AppUtil.getTalentId(request, response);
		int row = applicationDao.add(talentId, resumeId, positionId);
		// 转向页面
		if (row == 1) {
			request.setAttribute("success", "求职者的简历投递成功！");

		} else {
			request.setAttribute("fail", "求职者的简历投递失败！");
		}
		// 回显信息
		response.sendRedirect("PositionServlet?command=detail&id=" + positionId);

	}

	/**
	 * 根据职位id查询所有职位信息详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取页面传来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 调用业务逻辑
		PositionEntity position = new PositionDao().get(id);
		request.setAttribute("position", position);
		// 转向页面
		request.getRequestDispatcher("view/company/position-detail.jsp").forward(request, response);
	}

	/**
	 * 求职者申请信息的分页查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listTalentApplyByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNo = Constant.DEFAULT_PAGE_NO;
		// 非登录操作
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		int talentId = AppUtil.getTalentId(request, response);
		// 申请信息
		PageModel<ApplicationEntity> pageModel = applicationDao.listTalentApplyByPage(talentId, pageNo,
				Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		// 简历信息
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		// 转发 -> 优点：可以携带参数，缺点：地址上显示此次Servlet的请求地址，用户可以重复提交！
		request.getRequestDispatcher("view/talent/talent-home.jsp").forward(request, response);
	}

	/**
	 * 分页查询某个公司所有的职位申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listCompanyVerifyByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 把查询条件装到ApplicationEntity里面
		int companyId = AppUtil.getCompanyId(request, response);
		ApplicationEntity application = new ApplicationEntity();
		application.setCompany$id(companyId);
		// 表单查询表件
		String positionName = request.getParameter("positionName");
		// if (positionName != null && !"".equals(positionName)) {
		if (StringUtils.isNotBlank(positionName)) {
			application.setPosition$name(positionName);
		}
		String statusStr = request.getParameter("status");
		if (StringUtils.isNotBlank(statusStr)) {
			int status = Integer.parseInt(statusStr);
			application.setStatus(status);
		}

		int pageNo = Constant.DEFAULT_PAGE_NO;
		// 非登录操作
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}

		// 申请信息
		PageModel<ApplicationEntity> pageModel = applicationDao.listCompanyVerifyByPage(application, pageNo,
				Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		// 查询登录企业所发布的所有职位信息，并放到request里面，方便在页面上的显示
		List<PositionEntity> positionList = new PositionDao().listById(companyId);
		request.setAttribute("positionList", positionList);
		// 如果是表单查询条件，需要把查询条件回显
		request.setAttribute("applicationCondition", application);
		// 转发 -> 优点：可以携带参数，缺点：地址上显示此次Servlet的请求地址，用户可以重复提交！
		request.getRequestDispatcher("view/company/main.jsp").forward(request, response);
	}

	/**
	 * 企业审核求职者的申请
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void verify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int status = Integer.parseInt(request.getParameter("status"));
		int applicationId = Integer.parseInt(request.getParameter("applicationId"));
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));

		int row = applicationDao.verify(status, applicationId);

		// 转向
		response.sendRedirect("ApplicationServlet?command=listCompanyVerifyByPage&pageNo=" + pageNo);
	}

}
